1
|
|
|
/** |
2
|
|
|
redox |
3
|
|
|
Component that handles reduction/oxidation and ions. |
4
|
|
|
|
5
|
|
|
@namespace Components |
6
|
|
|
*/ |
7
|
|
|
'use strict'; |
8
|
|
|
|
9
|
|
|
angular.module('game').component('redox', { |
10
|
|
|
templateUrl: 'views/redox.html', |
11
|
|
|
controller: 'ct_redox', |
12
|
|
|
controllerAs: 'ct' |
13
|
|
|
}); |
14
|
|
|
|
15
|
|
|
angular.module('game').controller('ct_redox', ['state', 'data', 'visibility', 'util', 'format', 'reaction', |
16
|
|
|
function (state, data, visibility, util, format, reaction) { |
17
|
|
|
let ct = this; |
18
|
|
|
ct.state = state; |
19
|
|
|
ct.data = data; |
20
|
|
|
ct.util = util; |
21
|
|
|
ct.format = format; |
22
|
|
|
ct.reaction = reaction; |
23
|
|
|
|
24
|
|
|
function update(player) { |
25
|
|
|
for(let slot of player.element_slots){ |
26
|
|
|
if(!slot){ |
27
|
|
|
continue; |
28
|
|
|
} |
29
|
|
|
for (let redox of slot.redoxes) { |
30
|
|
|
if (!redox.resource || !redox.active) { |
31
|
|
|
continue; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
let reactant = ct.generateName(redox.element, redox.from); |
35
|
|
|
let power = ct.redoxPower(player); |
36
|
|
|
let number = Math.min(power, player.resources[reactant].number); |
37
|
|
|
let react = ct.redoxReaction(redox); |
38
|
|
|
|
39
|
|
|
ct.reaction.react(number, react, player); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/* Calculates the redox power based on the redox upgrades */ |
45
|
|
|
ct.redoxPower = function(player) { |
46
|
|
|
let level = player.global_upgrades.redox_bandwidth; |
47
|
|
|
let upgrade = data.global_upgrades.redox_bandwidth; |
48
|
|
|
let basePower = upgrade.power; |
49
|
|
|
let polynomial = upgrade.power_poly; |
50
|
|
|
return basePower * Math.floor(Math.pow(level, polynomial)); |
51
|
|
|
}; |
52
|
|
|
|
53
|
|
|
/* Writes a redox in the form of a reaction so that we can use the reaction |
54
|
|
|
service to process it */ |
55
|
|
|
ct.redoxReaction = function (redox) { |
56
|
|
|
let reactant = ct.generateName(redox.element, redox.from); |
57
|
|
|
let product = ct.generateName(redox.element, redox.to); |
58
|
|
|
let energy = redoxEnergy(redox.from, redox.to, redox.element); |
59
|
|
|
|
60
|
|
|
let react = { |
61
|
|
|
'reactant': {}, |
62
|
|
|
'product': {} |
63
|
|
|
}; |
64
|
|
|
|
65
|
|
|
react.reactant[reactant] = 1; |
66
|
|
|
react.product[product] = 1; |
67
|
|
|
if (energy > 0) { |
68
|
|
|
react.reactant.eV = energy; |
69
|
|
|
} else if (energy < 0) { |
70
|
|
|
react.product.eV = -energy; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
let electron = redox.from - redox.to; |
74
|
|
|
if (electron > 0) { |
75
|
|
|
react.reactant['e-'] = electron; |
76
|
|
|
} else if (electron < 0) { |
77
|
|
|
react.product['e-'] = -electron; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return react; |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
/* Calculates how much energy it takes to go from a redox level to another |
84
|
|
|
for a given element */ |
85
|
|
|
function redoxEnergy(from, to, element) { |
86
|
|
|
let energyFrom = data.redox[element][from]; |
87
|
|
|
let energyTo = data.redox[element][to]; |
88
|
|
|
let energy = energyTo - energyFrom; |
89
|
|
|
|
90
|
|
|
return energy; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* Generates the name of a ion, e.g. O3+ */ |
94
|
|
|
ct.generateName = function (element, i) { |
95
|
|
|
if (i === 0) { |
96
|
|
|
return data.elements[element].main; |
97
|
|
|
} |
98
|
|
|
let postfix = ''; |
99
|
|
|
if (Math.abs(i) > 1) { |
100
|
|
|
postfix = Math.abs(i); |
101
|
|
|
} |
102
|
|
|
postfix += getSign(i); |
103
|
|
|
let name = element + postfix; |
104
|
|
|
// special case!! H+ is just a proton |
105
|
|
|
if (name === 'H+') { |
106
|
|
|
name = 'p'; |
107
|
|
|
} |
108
|
|
|
return name; |
109
|
|
|
}; |
110
|
|
|
|
111
|
|
|
function getSign(number) { |
112
|
|
|
return number > 0 ? '+' : '-'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/* Calculates the number of redox slots based on the redox upgrades */ |
116
|
|
|
ct.redoxSlots = function (player) { |
117
|
|
|
let level = player.global_upgrades.redox_slots; |
118
|
|
|
let upgrade = data.global_upgrades.redox_slots; |
119
|
|
|
let basePower = upgrade.power; |
120
|
|
|
let multiplier = upgrade.power_mult; |
121
|
|
|
return basePower * Math.floor(multiplier * level); |
122
|
|
|
}; |
123
|
|
|
|
124
|
|
|
ct.redoxSize = function (player) { |
125
|
|
|
let size = 0; |
126
|
|
|
for(let slot of player.element_slots){ |
127
|
|
|
if(!slot){ |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
size += slot.redoxes.length; |
131
|
|
|
} |
132
|
|
|
return size; |
133
|
|
|
}; |
134
|
|
|
|
135
|
|
|
/* Adds a new redox to the player list */ |
136
|
|
|
ct.addRedox = function (player, slot) { |
137
|
|
|
if(ct.redoxSize(player) >= ct.redoxSlots(player)){ |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
slot.redoxes.push({ |
141
|
|
|
resource: data.elements[slot.element].main, |
142
|
|
|
active: false, |
143
|
|
|
element: slot.element, |
144
|
|
|
from: 0, |
145
|
|
|
to: 1 |
146
|
|
|
}); |
147
|
|
|
}; |
148
|
|
|
|
149
|
|
|
ct.removeRedox = function (slot, index) { |
150
|
|
|
slot.redoxes.splice(index, 1); |
151
|
|
|
}; |
152
|
|
|
|
153
|
|
|
ct.visibleRedox = function(slot) { |
154
|
|
|
return slot.redoxes; |
155
|
|
|
}; |
156
|
|
|
|
157
|
|
|
state.registerUpdate('redox', update); |
158
|
|
|
} |
159
|
|
|
]); |
160
|
|
|
|